//
//  XMAlertAction.m
//  MotoEye
//
//  Created by 徐盟 on 2023/4/20.
//

#import "XMAlertActionSheet.h"

@interface XMAlertActionSheet()

@property(nonatomic, weak) UIViewController *viewController;
@property(nonatomic, assign) UIAlertControllerStyle style;
@property(nonatomic, strong) NSMutableArray<UIAlertAction*>* actions;

@end

@implementation XMAlertActionSheet

- (NSMutableArray<UIAlertAction *> *)actions
{
    if(_actions ==nil){
        _actions = [NSMutableArray array];
    }
    return _actions;
}

+(instancetype) alertWithWiewController:(UIViewController *)viewController
{
    return [[self alloc]  initWithStyle:UIAlertControllerStyleAlert viewController:viewController];
}


+(instancetype) actionSheetWithViewController:(UIViewController *)viewController
{
    return [[self alloc]  initWithStyle:UIAlertControllerStyleActionSheet viewController:viewController];
}

-(instancetype) initWithStyle:(UIAlertControllerStyle)style viewController:(UIViewController *)viewController
{
    if(self = [super init]){
        self.viewController = viewController;
        self.style = style;
    }
    return self;
}

-(void) addCancelActionWithName:(NSString*)name actionBlock:(XMAlertActionActionBlock) block
{
    UIAlertAction *action = [UIAlertAction actionWithTitle:name style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        block();
    }];
    [self.actions addObject:action];
}
-(void) addDangerActionWithName:(NSString*)name actionBlock:(XMAlertActionActionBlock) block
{
    UIAlertAction *action = [UIAlertAction actionWithTitle:name style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        block();
    }];
    [self.actions addObject:action];
}
-(void) addActionWithName:(NSString*)name actionBlock:(XMAlertActionActionBlock) block
{
    UIAlertAction *action = [UIAlertAction actionWithTitle:name style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        block();
    }];
    [self.actions addObject:action];
}

-(void) show
{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:self.title message:self.message preferredStyle:self.style];
    [self.actions enumerateObjectsUsingBlock:^(UIAlertAction * _Nonnull action, NSUInteger idx, BOOL * _Nonnull stop) {
        [alertController addAction:action];
    }];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            //在另外一个线程中处理这些操作，然后通知主线程更新界面。
        dispatch_async(dispatch_get_main_queue(), ^{
            if(self.viewController){
                [self.viewController presentViewController:alertController animated:YES completion:nil];
            }
            
        });
    });

   
}

-(void(^)(NSString *, XMAlertActionActionBlock))toAddCancelAction
{
    return ^(NSString * name, XMAlertActionActionBlock block){
        [self addCancelActionWithName:name actionBlock:block];
    };
}
-(void(^)(NSString *, XMAlertActionActionBlock))toAddDangerAction
{
    return ^(NSString * name, XMAlertActionActionBlock block){
        [self addDangerActionWithName:name actionBlock:block];
    };
}
-(void(^)(NSString *, XMAlertActionActionBlock))toAddAction
{
    return ^(NSString * name, XMAlertActionActionBlock block){
        [self addActionWithName:name actionBlock:block];
    };
}
-(void(^)()) toShow
{
    return ^{
        [self show];
    };
}




@end
